//
// Created on 2024/9/7.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef OHOS_DEMO_PEER_CONNECTION_DEPDENCY_FACTORY_NATIVE_H
#define OHOS_DEMO_PEER_CONNECTION_DEPDENCY_FACTORY_NATIVE_H

#include "api/peer_connection_interface.h"
#include "rtc_base/thread.h"
#include "rtc_client/ohos_local_stream.h"
#include "rtc_client/nativecpp/local_stream.h"
#include "sdk/ohos/api/ohos_peer_connection_factory_interface.h"
#include "sdk/ohos/api/ohos_peer_connection_interface.h"
#include "rtc_client/capturer/njs_camera_capture.h"
#include "rtc_client/rtc_wrapper.h"

namespace ohosrtc {

using AudioTrackPtr = webrtc::scoped_refptr<webrtc::AudioTrackInterface>;
using VideoTrackPtr = webrtc::scoped_refptr<webrtc::VideoTrackInterface>;
using VideoSourcePtr = webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface>;
using PeerConnectionFactoryPtr = webrtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>;
using PeerConnectionObserver = webrtc::PeerConnectionObserver;
using ThreadPtr = std::unique_ptr<rtc::Thread>;
using AudioDeviceModulePtr = webrtc::scoped_refptr<webrtc::AudioDeviceModule>;
using LocalStreamPtr = std::shared_ptr<LocalStream>;
using PeerConnectionPtr = webrtc::scoped_refptr<webrtc::PeerConnectionInterface>;

// webrtc PeerConnectionFactory wrapper
class PeerConnectionDependencyFactoryNative {
  public:
    // create PeerConnectionFactory
    static std::shared_ptr<PeerConnectionDependencyFactoryNative> Create();
    // create PeerConnectionFactory
    void CreatePeerConnectionFactory();
    // create PeerConnection
    PeerConnectionPtr CreatePeerConnection(RTCConfig config, PeerConnectionObserver *observer);
    // create VideoSource for PeerConnection
    VideoSourcePtr CreateVideoSource(bool is_screen_cast);
    // create LocalVideoTrack for PeerConnection use VideoSource
    VideoTrackPtr CreateLocalVideoTrack(VideoSourcePtr video_source);
    // create LocalStream for PeerConnection
    LocalStreamPtr CreateLocalStream(bool has_audio, bool video_capturer, bool is_screen_cast);
    // create LocalAudioTrack for PeerConnection
    AudioTrackPtr CreateLocalAudioTrack();
    
    LocalStreamPtr GetLocalStream() {
      return local_stream_;
    }
  
    VideoSourcePtr GetVideoSource() {
      return video_source_;
    }
     
    VideoSourcePtr GetScreenSource() {
      return screen_source_;
    }
  
    PeerConnectionFactoryPtr GetPeerConnectionFactory() {
      return pc_factory_;
    }
  
    rtc::Thread* GetWorkerThread() {
      return worker_thread_.get();
    }
  
  private:
    std::string GenerateRandomId(int length);
    LocalStreamPtr CreateLocalStream();
    VideoSourcePtr video_source_;
    VideoSourcePtr screen_source_;  
    LocalStreamPtr local_stream_;  
  
    ThreadPtr signaling_thread_;
    ThreadPtr worker_thread_;
    ThreadPtr network_thread_;
    PeerConnectionFactoryPtr pc_factory_;
    AudioDeviceModulePtr adm_;
  
};
} // namespace ohosrtc

#endif // OHOS_DEMO_PEER_CONNECTION_DEPDENCY_FACTORY_NATIVE_H
